package com.example.cunwan_hd.Server;

import com.alibaba.fastjson.JSONObject;
import com.example.cunwan_hd.Mapper.DataMapper;
import com.example.cunwan_hd.Mapper.UserMapper;
import com.example.cunwan_hd.PaasContext;
import com.example.cunwan_hd.Pojo.Chat;
import com.example.cunwan_hd.Pojo.ChatMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import com.alibaba.fastjson.JSON;

@Component
@ServerEndpoint("/chat/{user}/{room}")

public class WebSocket {
    private static Map<String, Chat> rooms = new HashMap<>();
    private static Map<String, Session> seeions = new HashMap<>();
    @Autowired DataMapper datamapper =(DataMapper) PaasContext.getBean(DataMapper.class);

    @OnOpen
    public void OnOpen(Session session, @PathParam("user") String user, @PathParam("room") String room) {
        seeions.put(user, session);
        Chat chat = rooms.get(room);
        if (chat == null) {
            chat = new Chat(room);
            rooms.put(room, chat);

        }
        chat.addUser(user);
        onMessage((String) datamapper.get().get("data"),"system","1234");


    }

    @OnClose
    public void onClose(@PathParam("user") String user, @PathParam("room") String room) {
        seeions.remove(user); // 将当前会话对象从sessions中移除
        Chat chatRoom = rooms.get(room); // 根据room参数获取对应的聊天室对象
        if (chatRoom != null) {
            chatRoom.removeUser(user); // 将当前用户从聊天室的用户列表中移除
            System.out.println(user + " 离开了聊天室 " + room); // 打印日志信息
            onMessage((String) datamapper.get().get("data"),"system","1234");
        }
    }

    @Autowired
    public  Chatservice chatservice =(Chatservice) PaasContext.getBean(Chatservice.class);
    @Autowired
    UserMapper usermapper = (UserMapper) PaasContext.getBean(UserMapper.class);


    @OnMessage
    public void onMessage(String message, @PathParam("user") String user, @PathParam("room") String room) {
        System.out.println(user + " 在聊天室 " + room + " 发送了消息：" + message); // 打印日志信息
        ChatMessage chatMessage = new ChatMessage(user, null, message, new Date(),seeions.size()); // 创建一个聊天消息对象


        if (chatservice.chat(message)) {
            Chat chatRoom = rooms.get(room); // 根据room参数获取对应的聊天室对象
            if (chatRoom != null) {
                for (String u : chatRoom.getUsers()) { // 遍历聊天室的用户列表
                    Session s = seeions.get(u); // 根据用户名获取对应的会话对象
                    if (s != null && s.isOpen()) { // 如果会话对象存在且打开
                        try {

                            s.getBasicRemote().sendText(JSON.toJSONString(chatMessage)); // 将聊天消息对象转换为json字符串，并发送给客户端

                              DsService d=new DsService();
                              if(!user.equals("system")){
                                  d.setA(d.getA()+1);
                              }



                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

    }

    public  void  bro(String aaa){

    }


}
